//
//  DDUserCenterViewController.m
//  Dingdong
//
//  Created by 程强 on 16/11/11.
//  Copyright © 2016年 程强. All rights reserved.
//

#import "DDUserCenterViewController.h"
#import "DDAccountManagerViewController.h"
#import "DDLoginViewController.h"
#import "DDCouponViewController.h"
#import "DDIntegralViewController.h"
#import "DDOrderListViewController.h"
#import "DDFavouriteViewController.h"
#import "DDSettingViewController.h"
#import "DDEvaluateViewController.h"

@interface DDUserCenterViewController ()<UIScrollViewDelegate>
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *scrollWidth;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *scrollHeight;
@property (weak, nonatomic) IBOutlet UIView *orderBackView;
@property (weak, nonatomic) IBOutlet UIView *collectionBackView;
@property (weak, nonatomic) IBOutlet UIView *userInfoBgView;
@property (weak, nonatomic) IBOutlet UIView *signBgView;
@property (weak, nonatomic) IBOutlet UIImageView *photoImageView;
@property (weak, nonatomic) IBOutlet UILabel *nameLbl;
@property (weak, nonatomic) IBOutlet UILabel *vipLvLbl;

@end

@implementation DDUserCenterViewController
- (void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self loadUserInfo];
}
- (void)loadUserInfo
{
    MyUser;
    if ([SystemConfig shareInstance].isLogin) {
        self.signBgView.hidden = YES;
        self.userInfoBgView.hidden = NO;
    }else{
        self.userInfoBgView.hidden = YES;
        self.signBgView.hidden = NO;
    }
    self.nameLbl.text = myUser.uname;
    [self.photoImageView sd_setImageWithURL:[NSURL URLWithString:myUser.face] placeholderImage:nil];
    self.vipLvLbl.text = myUser.lvname;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self layoutCenterView];
    [self clearNavigationBar];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(loadUserInfo) name:@"LoadUserInfo" object:nil];
}
- (void)clearNavigationBar
{
    self.navBarView.backgroundColor = [UIColor clearColor];
}
- (void)layoutCenterView
{
    self.scrollWidth.constant = ScreenWidth;
    self.scrollHeight.constant = ScreenHeight+100;
    if ([SystemConfig shareInstance].isLogin) {
        self.userInfoBgView.hidden = NO;
        self.signBgView.hidden = YES;
    }else{
        self.userInfoBgView.hidden = YES;
        self.signBgView.hidden = NO;
    }
    
    UIImage *setIcon = [LoadImage(@"my_setting") imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UIBarButtonItem *rightBar = [[UIBarButtonItem alloc]initWithImage:setIcon style:UIBarButtonItemStylePlain target:self action:@selector(gotoSetting)];
    self.navigationItem.rightBarButtonItem = rightBar;
    
    _photoImageView.layer.masksToBounds = YES;
    _photoImageView.layer.cornerRadius =_photoImageView.bounds.size.width/2;
}
- (void)gotoSetting
{
    DDSettingViewController *vc = StoryBoardViewController(@"Setting", @"DDSettingViewController");
    [self.navigationController pushViewController:vc animated:YES];
}
#pragma mark --- IBAction
- (IBAction)accountManagerClick:(id)sender
{
    CheckLogin(self)
    DDAccountManagerViewController *vc = StoryBoardViewController(@"User", @"DDAccountManagerViewController");
    [self.navigationController pushViewController:vc animated:YES];
}
- (IBAction)signInClick:(id)sender
{
    weak_Self(self);
    UINavigationController *vc = StoryBoardViewController(@"Login", @"LoginNavi");
    DDLoginViewController *loginVC = (DDLoginViewController *)vc.topViewController;
    [self presentViewController:vc animated:YES completion:nil];
    [loginVC userLoginSuccessCallBack:^{
        weakSelf.userInfoBgView.hidden = NO;
        weakSelf.signBgView.hidden = YES;
    }];
}
- (IBAction)myFavouriteClick:(id)sender
{
    CheckLogin(self)
    DDFavouriteViewController *vc = StoryBoardViewController(@"Coupon", @"DDFavouriteViewController");
    [self.navigationController pushViewController:vc animated:YES];
}
- (IBAction)allOrderClick:(id)sender
{
    CheckLogin(self)
    DDOrderListViewController *vc = StoryBoardViewController(@"Order", @"DDOrderListViewController");
    vc.orderStatus = kOrderStatusAll;
    [self.navigationController pushViewController:vc animated:YES];
}
- (IBAction)pendingOrderClick:(id)sender
{
    CheckLogin(self)
    DDOrderListViewController *vc = StoryBoardViewController(@"Order", @"DDOrderListViewController");
    vc.orderStatus = kOrderStatusPending;
    [self.navigationController pushViewController:vc animated:YES];
}
- (IBAction)receiptOrderClick:(id)sender
{
    CheckLogin(self)
    DDOrderListViewController *vc = StoryBoardViewController(@"Order", @"DDOrderListViewController");
    vc.orderStatus = kOrderStatusShipped;
    [self.navigationController pushViewController:vc animated:YES];
}
- (IBAction)completeOrderClick:(id)sender
{
    CheckLogin(self)
    DDOrderListViewController *vc = StoryBoardViewController(@"Order", @"DDOrderListViewController");
    vc.orderStatus = kOrderStatusComplete;
    [self.navigationController pushViewController:vc animated:YES];
}
- (IBAction)myInterialClick:(id)sender
{
    CheckLogin(self)
    DDIntegralViewController *vc = StoryBoardViewController(@"Coupon", @"DDIntegralViewController");
    [self.navigationController pushViewController:vc animated:YES];
}
- (IBAction)myCouponClick:(id)sender
{
    CheckLogin(self)
    DDCouponViewController *vc = StoryBoardViewController(@"Coupon", @"DDCouponViewController");
    [self.navigationController pushViewController:vc animated:YES];
}
- (IBAction)settingClick:(id)sender
{
    DDSettingViewController *vc = StoryBoardViewController(@"Setting", @"DDSettingViewController");
    [self.navigationController pushViewController:vc animated:YES];
}
#pragma mark --- 导航栏变色
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat offsetY = scrollView.contentOffset.y;
    if (offsetY > NAVBAR_CHANGE_POINT) {
        CGFloat alpha = MIN(1, 1 - ((NAVBAR_CHANGE_POINT + 64 - offsetY) / 64));
        self.navBarView.backgroundColor = [kRedColor colorWithAlphaComponent:alpha];
        
    } else {
        self.navBarView.backgroundColor = [kRedColor colorWithAlphaComponent:0];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
